﻿<%@ WebHandler Language="C#" Class="roleMange" %>

using System;
using System.Text;
using System.Web;
using System.Collections.Generic;
using Chenxin.Lib.Hr.BLL;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;
using System.Collections;

//****************************************************************
//   臣信二手房
//   Copyright (c) 2010 Chenxin.                        
//****************************************************************
//****************************************************************      
//作成者       :  he.jifeng   
//创建日期     :  2010-02-26                 
//確認者       :  確認者          
//功能概要　   :  角色管理
//****************************************************************
public class roleMange : AjaxHandlerBase
{

    public roleMange()
    {
        //添加角色
        htTypePermit.Add("addrole", Constants.Permit_RoleAdd);
        //编辑角色
        htTypePermit.Add("editrole", Constants.Permit_RoleEdit);
        //删除角色
        htTypePermit.Add("delrole",Constants.Permit_RoleDel);
        //获得角色信息
        htTypePermit.Add("getroleinfo", Constants.Permit_RoleEdit);
        //获得权限组
        htTypePermit.Add("getpermits", Constants.Permit_PermitsAdd);
        //编辑权限组
        htTypePermit.Add("editpermits", Constants.Permit_PermitsAdd);
        //复制角色
        htTypePermit.Add("copyrole", Constants.Permit_RoleCopy);
        //复制公司角色
        htTypePermit.Add("companyrolecopy", Constants.Permit_RolesCopyCompany);
    }
    //*******************************************
    //功能概要       ：重写的请求处理方法
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：父类中隐藏了IHttpHandler接口方法，本类中须对本方法进行重写
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        //处理类型
        string strDealType = context.Request["op"].ToString();
        //响应字符串
        string strRet = "";

        //按类型处理
        switch (strDealType.ToLower())
        {
            //获取城市
            case "getcity":
                strRet = GetCitys();
                break;
            case "getcompany"://获取公司
                Guid cityId = (string.IsNullOrEmpty(context.Request["fatherid"])) ? Guid.Empty : new Guid(context.Request["fatherid"]);
                strRet = GetCompanysByCity(cityId);
                break;
            case "getall":
                strRet = getAllRoles(context);
                break;
            case "addrole":
                strRet = addRole(context);
                break;
            case "editpermits":
                strRet = editPermits(context);
                break;
            case "editrole":
                strRet = editRole(context);
                break;
            case "delrole":
                strRet = delRole(context);
                break;
            case "getroleinfo":
                strRet = getRoleById(context);
                break;
            case "getpermits":
                strRet = getPermitsById(context);
                break;
            case "role":        //得到所有角色的下拉框的内容
                strRet = GetRoleForCombo();
                break;
            case "copyrole":    //复制角色
                strRet = CopyRole(context);
                break;
            case "companyrolecopy":   //复制公司角色
                strRet = CopyRoleFromCity(context);
                break;
        }
        //响应
        context.Response.Write(strRet);
    }



    //*******************************************
    //功能概要       ：复制角色
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：复制一个角色
    //作成者         ：薛彦
    //作成日         ：2010-05-28 
    //*******************************************
    private string CopyRoleFromCity(HttpContext context)
    {
        string strRet;

        try
        {
            string companyid = context.Request["souce_companyid"];    //被复制的公司ID
            string companyname = context.Request["souce_companyname"];    // 被复制的公司名称
            
            string tocompanyid = context.Request["aim_companyid"];    //复制至的公司ID
            string tocompanyname = context.Request["aim_companyname"];    //复制至的公司名称

            string tocityid = context.Request["aim_cityid"];    //复制至的城市ID
            string tocityname = context.Request["aim_cityname"];    //复制至的城市名称
            
            Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();
            List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> list = bllBasePermitRoleDefine.GetModelList("CompanyId='" + companyid + "'");

            string strSql = "";
            if (list.Count > 0)
            {
                foreach (Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine r in list)
                {
                    Guid newguid = Guid.NewGuid();
                    strSql += string.Format("insert into Base_Permit_RoleDefine(RoleId,RoleName,CityId,CityName,CompanyId,CompanyName,IsShow,DefRole) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');", newguid, r.RoleName, tocityid, tocityname, tocompanyid, tocompanyname, r.IsShow.ToString(),r.DefRole.ToString());
                    strSql += string.Format("insert into Base_RolePermitionRelationInfo select newid(),'{0}',PermitId from Base_RolePermitionRelationInfo where RoleId='{1}';", newguid, r.RoleId.ToString());
                }

                Chenxin.Lib.Hr.DAL.Struct_UserDepartRelation.ExecuteSQL(strSql);

                //添加用户操作日志 2010-5-12 王玮 Add
                AddLog("" + CurrentUserInfo.UserName + "复制了公司角色:把公司[" + companyname + "]的角色复制给公司[" + tocompanyname + "]",
                        OperationType.OperationTypeComboList()["OprType_RoleCopy"].ToString());

                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HR);
                string strCacheKey = Chenxin.Lib.Cache.CacheDataType.HR_ALLROLES;
                //删除权限列表
                cs.Delete(strCacheKey);

                //执行成功
                strRet = retExecResult(Constants.ExecResult.Succ, "复制成功", "");
            }
            else
                strRet = retExecResult(Constants.ExecResult.Error, "复制公司无角色，复制失败！", "");
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //失败
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    

    //*******************************************
    //功能概要       ：复制角色
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：复制一个角色
    //作成者         ：薛彦
    //作成日         ：2010-05-28 
    //*******************************************
    private string CopyRole(HttpContext context)
    {
        string strRet;

        try
        {
            string roleid = context.Request["SouceRoleId"];
            //角色业务类
            Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();
            //角色实体类
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine entBasePermitRoleDefine =
            (Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine));

            entBasePermitRoleDefine.RoleId = Guid.NewGuid();

            bllBasePermitRoleDefine.Copy(entBasePermitRoleDefine,roleid);

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "复制了一个角色:公司[" + entBasePermitRoleDefine.CompanyName + "]角色名为[" + entBasePermitRoleDefine.RoleName + "]",
                    OperationType.OperationTypeComboList()["OprType_RoleCopy"].ToString());

            //执行成功
            strRet = retExecResult(Constants.ExecResult.Succ, "复制成功", "");
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //失败
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    

    //*******************************************
    //功能概要       ：根据城市ID得到所有角色
    //参数説明       ：
    //返回值説明     ：
    //功能詳細       ：根据城市ID得到所有角色
    //作成者         ：薛彦
    //作成日         ：2010-04-23
    //修改者         ：
    //修改日         ：
    //*******************************************
    private string GetRoleForCombo()
    {
        //业务角色类
        Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();

        //json数据
        StringBuilder shortListSb = new StringBuilder();

        //权限类
        List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> lsRoles = bllBasePermitRoleDefine.GetAllList();

        //循环列表
        for (int idxCnt = 0; idxCnt < lsRoles.Count; idxCnt++)
        {
            shortListSb.Append("{");
            shortListSb.AppendFormat("txt:'{0}',val:'{1}'", lsRoles[idxCnt].RoleName, lsRoles[idxCnt].RoleId);
            shortListSb.Append("},");
            
        }
        if (shortListSb.Length>0)
        {
            shortListSb.Remove(shortListSb.Length - 1, 1);
        }
        return string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", shortListSb.ToString());
    }

    //*******************************************
    //功能概要       ：得到所有角色
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：得到所有角色
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    private string getAllRoles(HttpContext context)
    {
        //返回字符串
        string strRet;
        string strParam;
           
        //业务角色类
        Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();
 
        try
        {
            //权限类
            //List<Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine> lsRoles = bllBasePermitRoleDefine.GetListByCity(CityId);
            Dictionary<string, string> dicParams = new Dictionary<string, string>();
            //全权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllRole))
            {
                dicParams.Add("Permit", Constants.Permit_ViewAllRole);
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityRole))
            {
                dicParams.Add("Permit", Constants.Permit_ViewCityRole);
                dicParams.Add("CurCityId", CurrentUserInfo.CityId.ToString());
            }
            //公司权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCompanyRole))
            {
                dicParams.Add("Permit", Constants.Permit_ViewCompanyRole);
                dicParams.Add("CurCompanyId", CurrentUserInfo.CompanyId.ToString());
            }
            
            //查看隐藏角色权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewHiddenRole))
            {
                dicParams.Add("Permit_Show", "All");
            }

            //当前页数
            strParam = context.Request.Params["pageindex"];
            if (strParam != null)
            {
                int intCurPage = Convert.ToInt32(strParam) + 1;
                dicParams.Add("PageIndex", intCurPage.ToString());
            }
            //显示行数
            strParam = context.Request.Params["pagesize"];
            if (strParam != null)
            {
                dicParams.Add("PageSize", strParam);
            }
            strParam = context.Request.Params["city"];
            if (strParam != null)
            {
                dicParams.Add("CityId", strParam);
            }
            strParam = context.Request.Params["company"];
            if (strParam != null)
            {
                dicParams.Add("CompanyId", strParam);
            }
            //显示状态
            strParam = context.Request.Params["showstatus"];
            if (!string.IsNullOrEmpty(strParam) && "0".Equals(strParam))
            {
                dicParams.Add("showStatus", "1");
            }
            System.Data.DataSet ds = bllBasePermitRoleDefine.GetPaginationData(dicParams);
            //输出权限信息
            strRet = retExecResult(Constants.ExecResult.Succ, "OK", crtHtmlOfAllRoles(ds));
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //相应ERROR
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：生成Html
    //参数説明       ：lsPermits 权限列表
    //返回值説明     ：Html显示字符串
    //功能詳細       ：根据得到的角色列表生成Hmtl显示
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    private Constants.Pagination crtHtmlOfAllRoles(System.Data.DataSet ds)
    {
        //Html结果
        StringBuilder strHtml = new StringBuilder();

        strHtml.Append("<table id='tbShowRole' class='PublicTable tabsorternew' style='text-align:center;width:100%'>");
        strHtml.Append("<thead>");
        strHtml.Append("<tr class='toolbar'>");
        strHtml.Append("<th>城市</th>");
        strHtml.Append("<th>公司</th>");
        strHtml.Append("<th>角色</th>");
        strHtml.Append("<th>操作</th>");
        strHtml.Append("</tr>");
        strHtml.Append("</thead>");
        strHtml.Append("<tbody>");
                                    
        //循环角色列表
        for (int idxRoles = 0; idxRoles < ds.Tables[0].Rows.Count; idxRoles++)
        {
            strHtml.AppendFormat("<tr id='{0}'>", ds.Tables[0].Rows[idxRoles]["RoleId"]);
            strHtml.AppendFormat("<td>{0}</td>", ds.Tables[0].Rows[idxRoles]["CityName"]);
            strHtml.AppendFormat("<td>{0}</td>", ds.Tables[0].Rows[idxRoles]["CompanyName"]);
            strHtml.AppendFormat("<td>{0}</td>", ds.Tables[0].Rows[idxRoles]["RoleName"]);
            strHtml.Append("<td>");
            strHtml.Append("<a class='icoEdit_n' href='#' name='aEditRole'>修改</a>");
            strHtml.Append("<a class='icoView_n' href='#' name='aEditPermits'>权限</a>");
            strHtml.Append("<a class='icoDelete_n' href='#' name='aDelRole'>删除</a>");
            strHtml.Append("</td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</tbody>");
        strHtml.Append("</table>");

        Constants.Pagination stuPag = new Constants.Pagination();
        stuPag.data = strHtml.ToString();
        stuPag.count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
        return stuPag;
    }

    //*******************************************
    //功能概要       ：添加角色
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：添加角色
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-03 
    //*******************************************
    private string addRole(HttpContext context)
    {
        string strRet;

        try
        {
            //角色业务类
            Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();
            //角色实体类
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine entBasePermitRoleDefine =
            (Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine));

            entBasePermitRoleDefine.RoleId = Guid.NewGuid();

            bllBasePermitRoleDefine.Add(entBasePermitRoleDefine);

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "添加了一个角色:城市[" + entBasePermitRoleDefine.CityName + "]角色名为[" + entBasePermitRoleDefine.RoleName + "]",
                    OperationType.OperationTypeComboList()["OprType_RoleAdd"].ToString());  
            
            //执行成功
            strRet = retExecResult(Constants.ExecResult.Succ, "添加成功", "");
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //失败
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：更新权限列表
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：更新权限列表
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-04 
    //*******************************************
    private string editPermits(HttpContext context)
    {
        //返回字符串
        string strRet;
       
        try
        {
            //角色权限关系业务类
            Chenxin.Lib.Hr.BLL.Base_RolePermitionRelationInfo bllBaseRolePermitionRelationInfo = new Chenxin.Lib.Hr.BLL.Base_RolePermitionRelationInfo();
            //角色权限关系实体类
            Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo entBaseRolePermitionRelationInfo;
            //角色Id
            string strRoleId = context.Request.Params["RoleId"];
            //权限列表
            string strLsPermits = context.Request.Params["lsPermits"];
            
            //权限列表数组
            string[] lsPermits = strLsPermits.Split(',');
            
            //全删除
            bllBaseRolePermitionRelationInfo.DeleteByRoleId(new Guid(strRoleId));

            //循环数组
            for (int idxCnt = 0; idxCnt < lsPermits.Length; idxCnt++)
            {
                if (lsPermits[idxCnt]!="")
                {
                    entBaseRolePermitionRelationInfo = new Chenxin.Lib.Hr.Entity.Base_RolePermitionRelationInfo();
                    entBaseRolePermitionRelationInfo.RecordId = Guid.NewGuid();
                    entBaseRolePermitionRelationInfo.RoleId = new Guid(strRoleId);
                    entBaseRolePermitionRelationInfo.PermitId = new Guid(lsPermits[idxCnt]);
                    //添加权限
                    bllBaseRolePermitionRelationInfo.Add(entBaseRolePermitionRelationInfo);
                }
            }

            //获取角色实体
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine clhebprd = Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetModel(new Guid(strRoleId));
            
            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "编辑了一个角色权限:城市[" + clhebprd.CityName + "]公司[" + clhebprd.CompanyName + "]角色名为[" + clhebprd.RoleName + "]的权限",
                    OperationType.OperationTypeComboList()["OprType_RolePermitsEdit"].ToString());
            
            //执行成功
            //strRet = JsonHelper<string>.WriteObject("SUCCESS", "");
            strRet = retExecResult(Constants.ExecResult.Succ, "保存成功", "");
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //失败
            //strRet = JsonHelper<string>.WriteObject("ERROR", ex.Message);
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    //*******************************************
    //功能概要       ：取得角色
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：按角色Id取得角色信息，该角色的权限列表，该角色分类的所属权限
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    private string getRoleById(HttpContext context)
    {
        string strRet;
        
        try
        {
            //结果集
            List<string> lsRet = new List<string>();
            //角色的业务处理类
            Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();

            //角色Id
            string strRoleId = context.Request.Params["RoleId"].ToString();
            
            //角色Model类
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine entBasePermitRoleDefine = bllBasePermitRoleDefine.GetModel(new Guid(strRoleId));

            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:{2}", ((int)Constants.ExecResult.Succ).ToString(), "OK",
            //                        Chenxin.Lib.Utility.JsonHelp.Convert2Json(entBasePermitRoleDefine)) + "}";
            strRet = retExecResult(Constants.ExecResult.Succ, "OK", entBasePermitRoleDefine);
            return strRet;
            
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //添加执行结果 
            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:\"\"", ((int)Constants.ExecResult.Error).ToString(), ex.Message) + "}";
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }

    }

    //*******************************************
    //功能概要       ：取得权限
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：按角色Id取得该角色的权限列表，该角色分类的所属权限
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-02 
    //*******************************************
    private string getPermitsById(HttpContext context)
    {
        string strRet;

        //角色Id
        string strRoleId = context.Request.Params["RoleId"].ToString();
        //返回结果
        StringBuilder strHtml = new StringBuilder();

        try
        {
            //角色权限关系业务类
            Chenxin.Lib.Hr.BLL.Full_RolePermitionRelationInfo bllFullRolePermitionRelationInfo = new Chenxin.Lib.Hr.BLL.Full_RolePermitionRelationInfo();
            //权限业务类
            Chenxin.Lib.Hr.BLL.Base_Permit_Define bllBasePermitDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_Define();
            
            //角色权限关系列表
            List<Chenxin.Lib.Hr.Entity.Full_RolePermitionRelationInfo> lsFullRolePermitionRelationInfo = bllFullRolePermitionRelationInfo.GetModelListByRoleId(new Guid(strRoleId));
            //分组权限表
            List<Chenxin.Lib.Hr.Entity.Base_Permit_Define> lsGroupPermits;
            //是否已被选中
            Boolean bSelected = false;
            //权限Id
            Guid gdPermitId;
            //权限名
            string strPermitNm;

            int ShowLevel;
            //全权限
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllRole))
            {
                ShowLevel = 10;
            }
            //城市权限
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityRole))
            {
                ShowLevel = 20;
            }
            //公司权限
            else
            {
                ShowLevel = 30;
            }
            
            //按分类分组的权限列表
            ArrayList lsPermits = bllBasePermitDefine.GetModelListDividedByType(ShowLevel);

            for (int idxCntOfAll = 0; idxCntOfAll < lsPermits.Count; idxCntOfAll++)
            {
                lsGroupPermits = (List<Chenxin.Lib.Hr.Entity.Base_Permit_Define>)lsPermits[idxCntOfAll];
                strHtml.Append("<div class='clsPermitType1'>");
                strHtml.Append(lsGroupPermits[0].PermitGroup);
                strHtml.Append("</div>");
                strHtml.Append("<div class='clsShowPermit1'>");
                strHtml.Append("<ul>");
                //循环分类权限列表
                for (int idxCntOfGroup = 0; idxCntOfGroup < lsGroupPermits.Count; idxCntOfGroup++)
                {

                    gdPermitId = lsGroupPermits[idxCntOfGroup].PermitID;
                    strPermitNm = lsGroupPermits[idxCntOfGroup].PermitName;

                    //初期化
                    bSelected = false;
                    
                    //循环列表
                    for (int idxCntOfSelected = 0; idxCntOfSelected < lsFullRolePermitionRelationInfo.Count; idxCntOfSelected++)
                    {
                        //判断是否被选中
                        if (gdPermitId == lsFullRolePermitionRelationInfo[idxCntOfSelected].PermitId)
                        {
                            bSelected = true;
                            break;
                        }
                    }

                    //选中
                    if (bSelected)
                    {
                        strHtml.Append("<li><input type='checkbox' name='chkPermts' value='" + gdPermitId + "' checked='checked' />" + strPermitNm + "</li>");
                    }
                    else
                    {
                        strHtml.Append("<li><input type='checkbox'  name='chkPermts' value='" + gdPermitId + "' />" + strPermitNm + "</li>");
                    }
                }
                strHtml.Append("</ul>");
                strHtml.Append("</div>");
            }

            strHtml.Append("<div class='clsShowPermit1'>");
            strHtml.Append("<ul>");
            strHtml.Append("<li><div class='btn'><a href='#' title='提交' id='btSaveRolePermits'><span>提  交</span></a></div></li>");

            strHtml.Append("<li><div class='btn'><a href='#' title='取消' id='btReturn'><span>取  消</span></a></div></li>");
            strHtml.Append("</ul>");
            strHtml.Append("</div>");
            
            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:{2}", ((int)Constants.ExecResult.Succ).ToString(), "OK", 
            //    Chenxin.Lib.Utility.JsonHelp.Convert2Json(strHtml.ToString())) + "}";
            strRet = retExecResult(Constants.ExecResult.Succ, "OK", strHtml.ToString());
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //添加执行结果 
            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:\"\"", ((int)Constants.ExecResult.Error).ToString(), ex.Message) + "}";
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }
    
    //*******************************************
    //功能概要       ：编辑角色
    //参数説明       ：context 会话上下文
    //返回值説明     ：无
    //功能詳細       ：编辑角色
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-03
    //*******************************************
    private string editRole(HttpContext context)
    {
        string strRet;
        
        try
        {         
            //角色业务类
            Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine bllBasePermitRoleDefine = new Chenxin.Lib.Hr.BLL.Base_Permit_RoleDefine();
            //角色实体类
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine entBasePermitRoleDefine =
            (Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine)Chenxin.Lib.Utility.JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine));

            //entBasePermitRoleDefine.RoleId = new Guid(context.Request.Params["RoleId"]);
            //entBasePermitRoleDefine.RoleName = context.Request.Params["RoleName"];
            //entBasePermitRoleDefine.CityId = new Guid(context.Request.Params["CityId"]);
            
            //数据更新
            bllBasePermitRoleDefine.Update(entBasePermitRoleDefine);

            //执行成功
            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:\"\"", ((int)Constants.ExecResult.Succ).ToString(), "OK") + "}";

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "编辑了一个角色:城市[" + entBasePermitRoleDefine.CityName + "]角色名为[" + entBasePermitRoleDefine.RoleName + "]",
                    OperationType.OperationTypeComboList()["OprType_RoleEdit"].ToString());  
            
            strRet = retExecResult(Constants.ExecResult.Succ, "更新成功", "");
            return strRet;
          
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //添加执行结果 
            //strRet = "{" + string.Format("success:\"{0}\",msg:\"{1}\",data:\"\"", ((int)Constants.ExecResult.Error).ToString(), ex.Message) + "}";
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //*******************************************
    //功能概要       ：删除角色
    //参数説明       ：context 会话上下文
    //返回值説明     ：无
    //功能詳細       ：删除角色
    //作成者         ：he.jifeng 
    //作成日         ：2010-03-03 
    //*******************************************
    private string delRole(HttpContext context)
    {
        string strRet;
        
        //角色-角色权限关系业务类
        Chenxin.Lib.Hr.BLL.Role_RolePermitionRelation bllRoleRolePermitionRelation = new Role_RolePermitionRelation();
        //角色Id
        string strRoleId = context.Request.Params["RoleId"].ToString();

        try
        {
            //获取角色实体
            Chenxin.Lib.Hr.Entity.Base_Permit_RoleDefine clhebprd = Chenxin.Lib.Hr.DAL.Base_Permit_RoleDefine.GetModel(new Guid(strRoleId));
            
            //删除
            bllRoleRolePermitionRelation.delRoleAndRolePermitRelation(new Guid(strRoleId));

            //添加用户操作日志 2010-5-12 王玮 Add
            AddLog("" + CurrentUserInfo.UserName + "删除了一个角色:城市[" + clhebprd.CityName + "]角色名为[" + clhebprd.RoleName + "]",
                    OperationType.OperationTypeComboList()["OprType_RoleDel"].ToString());  
            
            //执行成功
            strRet = retExecResult(Constants.ExecResult.Succ, "删除成功", "");
            return strRet;
        }
        catch (Exception ex)
        {
            //输出错误信息
            System.Console.Write(ex.Message);
            //执行失败
            strRet = retExecResult(Constants.ExecResult.Error, ex.Message, "");
            return strRet;
        }
    }

    //功能概要       ：获取所有城市列表
    //参数説明       ：context 会话上下文
    //返回值説明     ：无
    //功能詳細       ：获取所有城市列表
    //作成者         ：he.jifeng 
    //作成日         ：2010-05-13 
    //*******************************************
    private string GetCitys()
    {
        try
        {
            System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo> citylist = null;//new System.Collections.Generic.List<Chenxin.Lib.House.Entity.DataDictionary>();

            System.Text.StringBuilder str = new System.Text.StringBuilder();

            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllRole))
            {
                //首先从缓存中获取数据
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_CITYS);
                citylist = cs.GetList<Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo>(strCacheKey);

                if (!cs.Hit)//若缓存未命中，则从数据库中获取并存入缓存，时间为1天
                {
                    citylist = Chenxin.Lib.Hr.DAL.Struct_CityBaseInfo.GetListArray("");

                    cs.Set(strCacheKey, citylist, DateTime.Today.AddDays(1));
                }

                //生成JSON字符串
                foreach (Chenxin.Lib.Hr.Entity.Struct_CityBaseInfo item in citylist)
                {
                    str.Append("{ txt: '" + item.CityName.Trim() + "', val: '" + item.CityId.ToString() + "' },");
                }
                if (str.Length > 0) str.Remove(str.Length - 1, 1);
            }
            else
            {
                str.Append("{ txt: '" + CurrentUserInfo.CityName.Trim() + "', val: '" + CurrentUserInfo.CityId.ToString() + "' }");
            }

            return string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", str.ToString());
        }
        catch (Exception ex)
        {
            return retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }
    }

    /// <summary>
    /// 获取指定城市的公司列表
    /// </summary>
    /// <param name="cityId"></param>
    /// <returns></returns>
    private string GetCompanysByCity(Guid cityId)
    {
        try
        {
            System.Collections.Generic.List<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo> companylist = null;//new System.Collections.Generic.List<Chenxin.Lib.House.Entity.DataDictionary>();

            System.Text.StringBuilder str = new System.Text.StringBuilder();

            if (CurrentUserInfo.ContainsPermit(Constants.Permit_ViewAllRole) || CurrentUserInfo.ContainsPermit(Constants.Permit_ViewCityRole))
            {
                //首先从缓存中获取数据
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_STRUCT_TREE, cityId);
                companylist = cs.GetList<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCacheKey);

                if (!cs.Hit)//若缓存未命中，则从数据库中获取并存入缓存，时间为1天
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.AppendFormat("(DepartProperty = 200) AND (CityId = '{0}')", cityId);

                    string strOrder = " DepartName";
                    companylist = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetListArray(sb.ToString(), strOrder);

                    cs.Set(strCacheKey, companylist, DateTime.Today.AddDays(1));
                }


                //生成JSON字符串
                foreach (Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item in companylist)
                {
                    str.Append("{ txt: '" + item.DepartName.Trim() + "', val: '" + item.DepartId.ToString() + "' },");
                }
                if (str.Length > 0) str.Remove(str.Length - 1, 1);
            }
            else
            {
                str.Append("{ txt: '" + CurrentUserInfo.CompanyName.Trim() + "', val: '" + CurrentUserInfo.CompanyId.ToString() + "' }");
            }

            return string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", str.ToString());
        }
        catch (Exception ex)
        {
            return retExecResult(Constants.ExecResult.Error, ex.Message, "");
        }
    }
}